package com.system.boot.rbac.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.system.api.SuperInterface;
import com.system.api.rbac.entity.dto.UserFormDto;
import com.system.api.rbac.entity.dto.UserUpdateDto;
import com.system.api.rbac.entity.po.SysRole;
import com.system.api.rbac.entity.po.SysUserAccount;
import com.system.api.rbac.entity.search.RoleSearch;
import com.system.api.rbac.entity.search.UserSearch;
import com.system.boot.rbac.converter.UserConverter;
import com.system.boot.rbac.service.UserService;
import com.system.common.constant.SystemConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.yang.common.core.exception.BaseException;
import org.yang.common.core.result.BaseController;
import org.yang.common.core.result.R;
import org.yang.common.i18n.enums.RI18nEnum;
import org.yang.common.utils.jwt.JwtTokenAnalysisUtli;
import org.yang.database.minio.constant.MinioConstant;
import org.yang.database.minio.utils.MinioUtils;
import org.yang.database.orm.entity.BaseQuery;

import java.util.List;
import java.util.Map;

/**
 * B端用户
 * @author: lslands
 * @description:
 * @version：v1.0
 * @date: 2024/6/2 22:33
 */
@Slf4j
@RestController
@RequestMapping("/account")
@RequiredArgsConstructor
public class UserController extends BaseController {

    private final UserService userService;
    private final UserConverter userConverter;
    private final MinioUtils minioUtils;


    /**
     * 新增/修改用户
     * @param dto 操作对象
     */
    @PostMapping("/save")
    public R<?> saveOrUpdate(@RequestBody UserFormDto dto) {
        try{
            SysUserAccount userAccount = userConverter.formEntity(dto);
            if(null == userAccount.getCompanyId()){
                userAccount.setCompanyId(JwtTokenAnalysisUtli.companyId());
            }
            return toCreateAjax(userService.handInsert(userAccount, dto.getRoleIds()));
        }catch (BaseException e){
            return toCreateAjax(false);
        }
    }

    /**
     * 修改用户
     * @param dto
     */
    @PostMapping("/updateById")
    public R<?> update(@RequestBody UserUpdateDto dto){
        try{
            SysUserAccount userAccount = userConverter.formEntity(dto);
            return toUpdateAjax(userService.handUpdateById(userAccount, dto.getRoleIds()));
        }catch (BaseException e){
            return toUpdateAjax(false);
        }
    }


    /**
     * 分页模糊查询列表
     * @param search 分页查询参数
     * @return R<Page<SysUserAccount>>
     */
    @GetMapping("/page")
    public R<Page<SysUserAccount>> page(@ModelAttribute UserSearch search) throws BaseException {
        SysUserAccount record = userConverter.queryPoEntity(search);
        BaseQuery query = userConverter.queryEntity(search);
        record.setCompanyId(JwtTokenAnalysisUtli.companyId());
        Page<SysUserAccount> page = userService.getPage(query,record);
        List<SysUserAccount> list = page.getRecords().stream().map(x -> {
            try {
                ObjectMapper mapper = new ObjectMapper();
                Map map = mapper.readValue(x.getAvatar(), Map.class);
                String ht="http://192.168.1.83:9000/avatar/";
                x.setAvatar(ht+(String) map.get(MinioConstant.YOUNG_FILE_NAME));
                return x;
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }).toList();
        page.setRecords(list);
        return R.success(page);
    }
}
